﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace BizElements.Generator
{
    /// <summary>Provides common operation for text manipulation.</summary>
    internal static class TextUtil
    {
        #region EnsureSuffix.

        /// <summary>Checks whether the text ends with the specified suffix. If not, it will be appended.</summary>
        public static string EnsureSuffix(string text, string suffix)
        {
            if (text == null)
                text = "";

            if (!string.IsNullOrEmpty(suffix) && !text.EndsWith(suffix))
                text += suffix;

            return text;
        }

        #endregion

        #region RemovePrefix.

        /// <summary>Removes prefix.</summary>
        public static string RemovePrefix(string originalName, string prefixToRemove)
        {
            if (prefixToRemove != null && originalName.StartsWith(prefixToRemove))
                return originalName.Substring(prefixToRemove.Length);
            else
                return originalName;
        }

        /// <summary>Removes prefix.</summary>
        public static string RemovePrefix(string originalName, string prefixToRemove, bool ignoreCase)
        {
            if (prefixToRemove != null && originalName.StartsWith(prefixToRemove, ignoreCase, null))
                return originalName.Substring(prefixToRemove.Length);
            else
                return originalName;
        }

        #endregion

        #region RemoveSuffix.

        /// <summary>Safely removes the specified suffix from the given text.</summary>
        /// <param name="text">Text to trim. If <b>null</b> nothing happens, ie. <b>null</b> is returned.</param>
        /// <param name="suffix">Suffix to remove. If <b>null</b> or <see cref="String.Empty"/> nothing happens, ie. original value is returned.</param>
        public static string RemoveSuffix(string text, string suffix)
        {
            string trimmedText;
            if ((text != null) && (suffix != null) && text.EndsWith(suffix))
            {
                int cutAt = text.Length - suffix.Length;
                trimmedText = text.Substring(0, cutAt);
            }
            else
            {
                trimmedText = text;
            }

            return trimmedText;
        }

        #endregion

        #region ParseDictionary.

        /// <summary>
        /// Converts a string to a dictionary with keys and values of the specified types.
        /// </summary>
        /// <param name="keyType">The type of the keys in the dictionary.</param>
        /// <param name="valueType">The type of the values in the dictionary.</param>
        /// <param name="delimitedVariables">A string containing varibles (key-value pairs) to convert.</param>
        /// <param name="variableDelimiter">A string which delimites variables in the string.</param>
        /// <param name="keyValueDelimiter">A string which delimites a key from a value in a key-value pair.</param>
        /// <param name="formatter">An object which contains an <see cref="System.IFormatProvider"/> and 
        /// <see cref="DateTimeStyles"/> objects that supply culture-specific formatting information about <b>textRepresentation</b>.</param>
        /// <returns>Hashtable which contains the keys and values of the specified types. 
        /// Values which couldn't be parsed are represented with <b>null</b> references.</returns>
        public static Hashtable ParseDictionary(string delimitedVariables, string variableDelimiter, string keyValueDelimiter)
        {
            string[] keyValuePairs = delimitedVariables.Split(new string[] { variableDelimiter }, StringSplitOptions.None);
            Hashtable dictionary = new Hashtable(keyValuePairs.Length);
            foreach (string keyAndValue in keyValuePairs)
            {
                int firstDelimiterOccurrence = keyAndValue.IndexOf(keyValueDelimiter);
                if (firstDelimiterOccurrence < 0)
                    throw new InvalidOperationException("Key-value pair (" + keyAndValue + ") doesn't contain the specified key-value delimiter (" + keyValueDelimiter + ").");

                int keyLen = firstDelimiterOccurrence;
                string key = keyAndValue.Substring(0, keyLen);

                int delimiterLen = keyValueDelimiter.Length;
                int valueStart = firstDelimiterOccurrence + delimiterLen;
                string value = (valueStart < keyAndValue.Length) ? keyAndValue.Substring(valueStart, keyAndValue.Length - valueStart) : string.Empty;

                dictionary[key] = value;
            }

            return dictionary;
        }

        #endregion
    }
}
